package com.wdh.dbutil;

import cn.hutool.core.io.file.FileReader;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.*;

public class App {

    public static void main(String[] args)throws Exception {
        App app = new App();
        String redisHosts = "10.16.1.145:6379,10.16.1.146:6380,10.16.1.147:6381,10.16.1.146:6379,10.16.1.147:6380,10.16.1.145:6381";
        String redisPass = "Ykzx@rcredis";
        app.updateRcuCache(redisHosts,redisPass);
    }

    public void updateRcuCache(String redisCfg,String redisPass)throws Exception{
        Set<HostAndPort> nodes = new HashSet<>();
        String[] arr = redisCfg.split(",");
        for (int i = 0; i < arr.length; i++) {
            String[] item = arr[i].split(":");
            String host = item[0];
            String port = item[1];
            nodes.add(new HostAndPort(host,Integer.parseInt(port)));
        }
        JedisCluster jedisCluster = null;
        if( StringUtils.isNotEmpty(redisPass) ){
            jedisCluster = new JedisCluster(nodes,2000,2000,5,redisPass, new GenericObjectPoolConfig());
        }else{
            jedisCluster = new JedisCluster(nodes,2000,2000,5, new GenericObjectPoolConfig());
        }

        Map<String, String> rsuMap = loadRsu();
        Map<String, String> rcuRelMap = loadRcuRsuRelation();
        Map<String, String> rcuMap = jedisCluster.hgetAll("rcu_base_data");
        for (Map.Entry<String, String> entry : rcuMap.entrySet()) {
            String rcuId = entry.getKey();
            String value = entry.getValue();
            JSONObject jsonValue = JSON.parseObject(value);
            String rsuId = rcuRelMap.get(rcuId);
            if( rsuId == null ){
                System.out.println(String.format("not found %s",rcuId));
                continue;
            }
            String rsuSerialNum = rsuMap.get(rsuId);
            JSONObject rsu = new JSONObject();
            rsu.put("rsuId",rsuId);
            rsu.put("serialNum",rsuSerialNum);
            JSONArray rsuList = new JSONArray();
            rsuList.add(rsu);
            jsonValue.put("rsuLst",rsuList);
            System.out.println(String.format("%s,%s,%s",rcuId,rsuId,rsuSerialNum));
            rcuMap.put(rcuId,jsonValue.toJSONString());
        }
        jedisCluster.hmset("rcu_base_data",rcuMap);
    }

    public  void writeRedis(String redisCfg,String redisPass)throws Exception{
        Set<HostAndPort> nodes = new HashSet<>();
        String[] arr = redisCfg.split(",");
        for (int i = 0; i < arr.length; i++) {
            String[] item = arr[i].split(":");
            String host = item[0];
            String port = item[1];
            nodes.add(new HostAndPort(host,Integer.parseInt(port)));
        }
        JedisCluster jedisCluster = null;
        if( StringUtils.isNotEmpty(redisPass) ){
            jedisCluster = new JedisCluster(nodes,2000,2000,5,redisPass, new GenericObjectPoolConfig());
        }else{
            jedisCluster = new JedisCluster(nodes,2000,2000,5, new GenericObjectPoolConfig());
        }

        FileReader reader = new FileReader("/home/ykzx/wdh/redisData.txt");
        List<String> lines = reader.readLines();
        for (String line : lines) {
            String[] split = line.split("######");
            String key = split[0];
            String value = split[1];
            System.out.println("=============> " + key);
            JSONObject json = JSON.parseObject(value);
            Set<String> keys = json.keySet();
            Map<String,String> dataMap = new HashMap<>();
            for (String k : keys) {
                System.out.println(k  + "," + json.getString(k));
                dataMap.put(k,json.getString(k));
            }
            jedisCluster.hmset(key,dataMap);
        }
    }

    public void readRedis(String redisHosts,String redisPass,String[] keys)throws Exception{
        Set<HostAndPort> nodes = new HashSet<>();
        String[] arr = redisHosts.split(",");
        for (int i = 0; i < arr.length; i++) {
            String[] item = arr[i].split(":");
            String host = item[0];
            String port = item[1];
            nodes.add(new HostAndPort(host,Integer.parseInt(port)));
        }
        JedisCluster jedisCluster = null;
        if( StringUtils.isNotEmpty(redisPass) ){
            jedisCluster = new JedisCluster(nodes,2000,2000,5,redisPass, new GenericObjectPoolConfig());
        }else{
            jedisCluster = new JedisCluster(nodes,2000,2000,5, new GenericObjectPoolConfig());
        }

       // Map<String, String> rcuMap = loadRcuRsuRelation();
        List<String> lines = new ArrayList<>();
        for (String key : keys) {
            Map<String, String> map = jedisCluster.hgetAll(key);
            Set<String> keysSet = map.keySet();
            for (String k : keysSet) {
                System.out.println("======================");
                System.out.println(k);
                System.out.println(map.get(k));
            }
           // String line = key + "######" + JSON.toJSONString(map);
           // lines.add(line);
        }

       // FileWriter writer = new FileWriter("d:/redisData.txt");
        //writer.writeLines(lines);
    }

    /**
     * key  rcu
     * value rsu
     * */
    private Map<String,String> loadRcuRsuRelation()throws Exception{
        Map<String,String> map = new HashMap<>();
        FileReader reader = new FileReader("/home/ykzx/wdh/rcu.txt");
        List<String> lines = reader.readLines();
        lines.forEach(line ->{
            String[] split = line.split(",");
            String key = split[1];
            String value = split[2];
            if( key.startsWith("U") ){
                map.put(key,value);
            }else{
                map.put(value,key);
            }
        });

        return map;
    }

    /**
     * key  devId
     * value serialNum
     * */
    private Map<String,String> loadRsu()throws Exception{
        Map<String,String> map = new HashMap<>();
        FileReader reader = new FileReader("/home/ykzx/wdh/rcu.txt");
        List<String> lines = reader.readLines();
        lines.forEach(line ->{
            String[] split = line.split(",");
            String key = split[1];
            String value = split[2];
            String serialNum = split[3];
            if( key.startsWith("U") ){
                map.put(value,serialNum);
            }else{
                map.put(key,serialNum);
            }
        });

        return map;
    }
}
